﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.View.TabForms.Handles;


namespace HR60_PanelSoft.View.TabForms.ListItems
{
	/// <summary>
	/// 单控设备场景控制列表
	/// 主要提供修改与删除SceneEvent两个事件
	/// </summary>
	public partial class SceneDeviceList : BaseFormControl
	{
		public SceneDeviceList()
		{
			InitializeComponent();
		}

		private Scene scene = null;

		/// <summary>
		/// 场景中单控设备事件改变
		/// </summary>
		public event DeviceEventChangeHandle DeviceEventChange;

		// 目标场景
		public Scene TagScene
		{
			get
			{
				return scene;
			}
		}

		// 初始化界面
		public void init(Scene scene)
		{
			this.scene = scene;
			selGroup.bindData((IList)deviceGroups, "name");
		}

		// 刷新单控设备列表
		private void refreshDeviceList(DeviceGroup group)
		{
			CatchValueChange = false;

			IList<Device> devs = group.findButtonCtrlDevices();
			tblList.Controls.Clear();
			tblList.RowCount = devs.Count;
			foreach (Device dev in devs)
			{
				SceneDeviceItem item = new SceneDeviceItem(dev);
				SceneEvent se = scene.findEventByDevice(dev.uuid);
				item.Value = se;
				item.AutoSubmitFlag = false;
				tblList.Controls.Add(item);

				item.ValueChanged += new EventHandler(item_ValueChanged);
			}

			CatchValueChange = true;
		}

		void item_ValueChanged(object sender, EventArgs e)
		{
			SceneDeviceItem item = ((SceneDeviceItem)sender);
			SceneEvent se = (SceneEvent)item.Value;

			// 场景事件发生改变
			if (DeviceEventChange != null) DeviceEventChange(item.TagDevice, se);
		}

		// 设备分组选择事件
		private void selGroup_ValueChanged(object sender, EventArgs e)
		{
			if (selGroup.Value != null) 
				refreshDeviceList((DeviceGroup)selGroup.Value);
		}

		// 全开
		private void btnOpen_Click(object sender, EventArgs e)
		{
			foreach (Control control in tblList.Controls)
			{
				SceneDeviceItem item = (SceneDeviceItem)control;
				item.open();
			}
		}

		// 全关
		private void btnClose_Click(object sender, EventArgs e)
		{
			foreach (Control control in tblList.Controls)
			{
				SceneDeviceItem item = (SceneDeviceItem)control;
				item.close();
			}
		}



	}
}
